package com.hjq.mallproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hjq.mallproduct.service.CategoryBrandRelationService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjq.common.utils.PageUtils;
import com.hjq.common.utils.Query;

import com.hjq.mallproduct.dao.BrandDao;
import com.hjq.mallproduct.entity.BrandEntity;
import com.hjq.mallproduct.service.BrandService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;

@Transactional
@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
@Autowired
    CategoryBrandRelationService categoryBrandRelationservice;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");
        if(StringUtils.isEmptyOrWhitespaceOnly(key)){
            IPage<BrandEntity> page = this.page(
                    new Query<BrandEntity>().getPage(params),
                    new QueryWrapper<BrandEntity>()
            );

            return new PageUtils(page);
        }
        else {
            QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
            wrapper.eq("brand_id",key).or().like("name",key).or().like("descript",key);
            IPage<BrandEntity> page = this.page(
                    new Query<BrandEntity>().getPage(params),
                    wrapper
            );

            return new PageUtils(page);
        }

    }

    @Override
    public void updateDetail(BrandEntity brand) {
        //保证冗余字段的数据一致
        Long brandId = brand.getBrandId();
        String name = brand.getName();
        this.updateById(brand);
        //更新关联表数据
        if(!StringUtils.isEmptyOrWhitespaceOnly(name)){
            categoryBrandRelationservice.updateBrand(brandId,name);

            //TODO 其他关联

        }



    }

}